<template>
  <j-modal
    :title="titles"
    :width="1000"
    :visible="visible"
    :confirmLoading="confirmLoading"
    switchFullscreen
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
    :footer="status == 1 ? false : status == 5 ? false : departstatus == 4 ? false : tr "
  >
    <a-spin :spinning="confirmLoading">
      <div v-if="status == 2">
        <a-form-model ref="form" :model="model" :rules="validatorRules">
          <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            prop="userConsultation"
            label="咨询用户"
          >
            <a-input
              :disabled="departstatus == 4 ? true : false"
              placeholder="请输入咨询用户"
              v-model="model.userConsultation"
            />
          </a-form-model-item>
          <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            prop="consultantExpert"
            label="咨询专家"
          >
            <a-input
              :disabled="true"
              placeholder="请输入咨询专家"
              v-model="model.consultantExpert"
            />
          </a-form-model-item>
          <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            prop="questionnaireLink"
            label="问卷链接"
          >
            <a-input
              :disabled="departstatus == 4 ? true : false"
              placeholder="请输入问卷链接"
              v-model="model.questionnaireLink"
            />
          </a-form-model-item>
          <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            prop="consultTime"
            label="咨询时间"
          >
            <a-date-picker
              :disabled="departstatus == 4 ? true : false"
              style="width: 100%"
              showTime
              valueFormat="YYYY-MM-DD HH:mm:ss"
              v-model="model.consultTime"
            />
          </a-form-model-item>
          <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            prop="heSaidPrice"
            label="他说币"
          >
            <a-input-number
              :disabled="true"
              v-model="model.heSaidPrice"
              style="width: 100%"
              :max="99999"
              :min="0"
              :parser="parseValue"
            />
          </a-form-model-item>
        </a-form-model>
      </div>
      <div v-else-if="status == 1">
        <a-descriptions>
          <a-descriptions-item label="咨询用户">
            {{ model.userConsultation }}
          </a-descriptions-item>
          <a-descriptions-item label="咨询专家">
            {{ model.consultantExpert }}
          </a-descriptions-item>
          <a-descriptions-item label="问卷链接">
            {{ model.questionnaireLink }}
          </a-descriptions-item>
          <a-descriptions-item label="咨询时间">
            {{ model.consultTime }}
          </a-descriptions-item>
          <a-descriptions-item label="他说币">
            {{ model.heSaidPrice }}
          </a-descriptions-item>
          <a-descriptions-item label="咨询状态">
            {{
              model.advisoryStatus == 0
                ? "预约中"
                : model.advisoryStatus == 1
                ? "预约成功"
                : model.advisoryStatus == 2
                ? "预约失败"
                : "预约完成"
            }}
          </a-descriptions-item>
        </a-descriptions>
      </div>
      <div v-else-if="status == 0">
        <a-form-model ref="form" :model="model" :rules="validatorRules">
          <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            prop="openid"
            label="指派人(可搜索)"
          >
            <a-select
              showSearch
              v-model="model.consultantId"
              placeholder="请选择手机号"
              @search="searchChange($event)"
              @change="handelchange"
              option-filter-prop="children"
              :filter-option="filterOption"
            >
              <a-select-option
                :value="item.id"
                v-for="(item, index) in list"
                :key="index"
                >{{ item.username }}</a-select-option
              >
            </a-select>
          </a-form-model-item>
          <!-- <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            prop="expertPhone"
            label="手机号"
          >
            <a-input
              v-model="model.expertPhone"
              style="width: 100%;"
              :disabled="true"
            />
          </a-form-model-item> -->
        </a-form-model>
      </div>
      <div v-else-if="status == 4">
        <a-form-model ref="form" :model="model" :rules="validatorRules">
          <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            prop="orderNumber"
            label="订单编号"
          >
            <a-input
              v-model="model.orderNumber"
              style="width: 100%;"
              :disabled="true"
            />
          </a-form-model-item>
          <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            prop="orderTime"
            label="订单时间"
          >
            <a-input
              :disabled="true"
              v-model="model.orderTime"
              style="width: 100%;"
            />
          </a-form-model-item>
          <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            prop="orderStatus"
            label="订单状态"
          >
            <a-select
              :disabled="true"
              v-model="model.orderStatus"
              placeholder="请选择帖子类型"
            >
              <a-select-option
                :value="item.id"
                v-for="(item, index) in arr"
                :key="index"
                >{{ item.status }}</a-select-option
              >
            </a-select>
          </a-form-model-item>
          <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            prop=" consultationTime"
            label="咨询时间"
          >
            <a-input
              :disabled="true"
              v-model="model.consultationTime"
              style="width: 100%;"
            />
          </a-form-model-item>
          <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            prop="orderNumber"
            label="订单编号"
          >
            <a-input
              :disabled="true"
              v-model="model.orderNumber"
              style="width: 100%;"
            />
          </a-form-model-item>
          <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            prop="needHeSaidPrice"
            label="需支付他币"
          >
            <a-input-number
              :disabled="true"
              :max="99999"
              :min="0"
              v-model="model.needHeSaidPrice"
              style="width: 100%;"
            />
          </a-form-model-item>
          <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            prop="heSaidPrice"
            label="客户支付他币"
          >
            <a-input-number
              :max="99999"
              :min="0"
              :parser="parseValue"
              v-model="model.heSaidPrice"
              style="width: 100%;"
            />
          </a-form-model-item>
          <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            prop="consultationDuration"
            label="咨询时长（分钟）"
          >
            <a-input-number
              v-model="model.consultationDuration"
              style="width: 100%;"
              :max="99999"
              :min="1"
              :parser="parseValue"
            />
          </a-form-model-item>
          <a-form-model-item
            :labelCol="labelCol"
            :wrapperCol="wrapperCol"
            prop="consultationUnitPrice"
            label="咨询单价"
          >
            <a-input-number
              :max="99999"
              :min="1"
              :parser="parseValue"
              v-model="model.consultationUnitPrice"
              style="width: 100%;"
            />
          </a-form-model-item>
        </a-form-model>
      </div>
      <div v-else-if="status == 5">
        <a-descriptions bordered>
          <a-descriptions-item label="订单编号">
            {{ model.orderNumber }}
          </a-descriptions-item>
          <a-descriptions-item label="订单时间">
            {{ model.orderTime }}
          </a-descriptions-item>
          <a-descriptions-item label="订单状态">
            {{
              model.orderStatus == 0
                ? "待支付"
                : model.orderStatus == 1
                ? "已支付"
                : "已关闭"
            }}
          </a-descriptions-item>
          <a-descriptions-item label="需支付他币">
            {{ model.needHeSaidPrice }}
          </a-descriptions-item>
          <a-descriptions-item label="客户支付他币">
            {{ model.heSaidPrice }}
          </a-descriptions-item>
        </a-descriptions>
      </div>
    </a-spin>
  </j-modal>
</template>

<script>
import { httpAction } from "@/api/manage";
import {
  busConsultassign,
  paymentOrderInfoedit,
  generateEnd,
  paymentOrderInfoad,
} from "@/api/tsapi";
import moment from "moment";
import { postAction } from "../../../../api/manage";

export default {
  name: "ConsultModal",
  data() {
    return {
      titles: "",
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 },
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 },
      },

      confirmLoading: false,
      validatorRules: {},
      url: {
        add: "/consult/consult/add",
        edit: "/consult/consult/edit",
      },
      status: "",
      list: [],
      arr: [
        {
          id: 0,
          status: "待支付",
        },
        {
          id: 1,
          status: "已支付",
        },
        {
          id: 2,
          status: "已关闭",
        },
      ],
      useropenid: "",
      messageobj: {},
      departstatus: "",
    };
  },
  created() {
    // 获取指派用户
    this.Userdata();
  },
  methods: {
    Userdata(e) {
      let params = {
        phone: e,
      };
      busConsultassign(params).then((res) => {
        console.log(res.result.records, "rrrrr");
        res.result.records.map((res) => {
          res.username = res.username + `(${res.phone})`;
        });

        this.list = res.result.records;
        console.log(this.list,'list');
      });
    },
    filterOption(input, option) {
      return (
        option.componentOptions.children[0].text
          .toLowerCase()
          .indexOf(input.toLowerCase()) >= 0
      );
    },
    //下拉搜索
    searchChange(e) {
      this.Userdata(e);
    },
    add() {
      //初始化默认值
      this.edit({});
    },
    edit(record, type) {
      console.log(type, "type");

      this.status = type;
      this.visible = true;
      if (type == 1) {
        this.titles = "查看";
        this.model = Object.assign({}, record);
      } else if (type == 2) {
        this.model = Object.assign({}, record);
        this.departstatus = record.advisoryStatus;
        console.log(record, "record");
        this.titles = "编辑";
      } else if (type == "4") {
        this.model = Object.assign({}, record);

        this.messageobj = record;
        this.titles = "支付订单";
      } else if (type == "5") {
        this.model = Object.assign({}, record);
        this.titles = "订单信息";
      } else {
        const idx = this.list.findIndex(
          (item) => item.phone == record.userPhone
        );
        // this.list.splice(idx, 1);
        this.list = this.list;
        this.model = Object.assign({}, record);
        // this.model.consultantId = record.consultantId + `(${record.expertPhone})`
        this.titles = "指派";
        // const ids = this.list.find((item) => item.openid == record.openid);
        // this.model.openid = ids.openid;
      }
    },
    close() {
      this.$emit("close");
      this.visible = false;
      if (this.status == 2) {
        this.$refs.form.clearValidate();
      }
      this.Userdata();
    },
    handleOk() {
      console.log(this.model,'llll');
      const that = this;
      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true;
          let httpurl = "";
          let method = "";
          if (!this.model.id) {
            httpurl += this.url.add;
            method = "post";
          } else {
            httpurl += this.url.edit;
            method = "post";
            // this.model["openid"] = this.useropenid;
          }
          console.log(that.model,'11111');
          //根据状态4判断为修改支付订单
          if (this.status == "4") {
            let busConsult = localStorage.getItem("busConsult");
            console.log(JSON.parse(busConsult), "busConsult");

            paymentOrderInfoad(this.model).then((res) => {

              if (res.code == 200) {
                that.$message.success("生成订单成功");
                that.$emit("ok");
                that.confirmLoading = false;
                that.close();
              } else {
                that.$message.warning(res.message);
              }
            });
            // paymentOrderInfoedit(that.model).then((res) => {
            //   if (res.code == 200) {
            //     that.$message.success("生成订单成功");
            //     that.$emit("ok");
            //     that.confirmLoading = false;
            //     that.close();
            //   } else {
            //     that.$message.warning(res.message);
            //   }
            // });
            //生成订单后结束咨询
            generateEnd(JSON.parse(busConsult)).then((res) => {});
          } else {
            httpAction(httpurl, that.model, method)
              .then((res) => {
                if (res.success) {
                  that.$message.success(res.message);
                  that.$emit("ok");
                  this.useropenid = "";
                } else {
                  that.$message.warning(res.message);
                }
              })
              .finally(() => {
                that.confirmLoading = false;
                that.close();
                that.Userdata();
              });
          }
        } else {
          return false;
        }
      });
    },
    handleCancel() {
      this.close();
      this.useropenid = "";
    },
    //限制年龄小数点和负数
    parseValue(value) {
      // 如果输入值不是数字或者小数，则返回0
      if (isNaN(value) || value.indexOf(".") !== -1 || value < 0) {
        this.$message.warning("不能大于99999!");
        value.match(/^\d{0,5}/)[0];

        return 0;
      }

      return value;
    },
    handelchange(e, data) {
      console.log(e, data, "eeeeeeeeeeeeeeeee");
      const ids = this.list.find((item) => item.id == e);
      this.useropenid = ids.openid;
      this.model.expertPhone = ids.phone;
      console.log(ids, "ids");
    },
  },
};
</script>

<style lang="less" scoped></style>
